package com.tubruk.trackme;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.tubruk.object.PopMenu;
import com.tubruk.util.dialog.ListMenu;

public class Start extends MapActivity {
    private MyLocationListener locListener;
	
	private ArrayList<PopMenu> options = null;
	
	private static final int DIALOG_OPTIONS = 1;
	
	private static final String RECORDING_START = "   Start   ";
	private static final String RECORDING_STOP  = "   Stop    ";
	
	private boolean recording;
	
	private static final int VIEW_DEFAULT = 0;
	private static final int VIEW_SATELLITE = 1;
	
	private int viewType;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        viewType = Start.VIEW_DEFAULT;
        
        options = new ArrayList<PopMenu>();
		options.add(new PopMenu(PopMenu.NO_IMAGE, "  History"));
		options.add(new PopMenu(PopMenu.NO_IMAGE, "  Preferences"));
		options.add(new PopMenu(PopMenu.NO_IMAGE, "  Run in Background"));
		options.add(new PopMenu(PopMenu.NO_IMAGE, "  Close Menu"));
		options.add(new PopMenu(PopMenu.NO_IMAGE, "  EXIT"));
		
		Button resultBtn = (Button) this.findViewById(R.id.result);
		resultBtn.setOnClickListener(new ResultClickHandler());
	
		Button recordBtn = (Button) this.findViewById(R.id.record);
		recordBtn.setText(Start.RECORDING_START);
		recording = false;
		recordBtn.setOnClickListener(new RecordClickHandler());
		
		Button toggleBtn = (Button) this.findViewById(R.id.toggleview);
		toggleBtn.setOnClickListener(new ToggleViewHandler());
	
		MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
    
     // set up the LocationManager
        
        List<Overlay> mapOverlays = mapView.getOverlays();
        
        
        LocationManager myManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        MapController mapController = mapView.getController();
        
        //locListener = new MyLocationListener(this.getResources(), myManager, mapController, mapOverlays);
        
        locListener.startListening();
        
    }
    
    public boolean onCreateOptionsMenu(Menu menu) {
    	showDialog(Start.DIALOG_OPTIONS);
    	return false;
    }
    
    private class ToggleViewHandler implements View.OnClickListener {
    	public void onClick(View v){
    		
    		MapView mapView = (MapView) findViewById(R.id.mapview);
            
    		switch(viewType) {
	    		case Start.VIEW_DEFAULT:
	    			viewType = Start.VIEW_SATELLITE;
	    			mapView.setSatellite(true);
	    	        //mapView.setStreetView(true);
	    			break;
	    		case Start.VIEW_SATELLITE:
	    			viewType = Start.VIEW_DEFAULT;
	    			mapView.setSatellite(false);
	    			//mapView.setStreetView(false);
	    			break;
	    		}
    		
    	}
    }
    
    private class ResultClickHandler implements View.OnClickListener {
		public void onClick(View v) {
			//showDialog(Start.DIALOG_OPTIONS);
			Intent i = new Intent(Start.this, Finish.class);
			startActivity(i);//, Global.ACTIVITY_FINISH);
			
		}
	}
    
    private class RecordClickHandler implements View.OnClickListener {
		public void onClick(View v) {
			
			if (recording) {
				new AlertDialog.Builder(Start.this).setIcon(
						android.R.drawable.ic_dialog_alert)
						.setTitle("Recording")
						.setMessage("You are about to stop recording. Are you sure?")
						.setPositiveButton("Yes",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,int which) {
									
									//stop recording and show result
									Intent i = new Intent(Start.this, Finish.class);
									startActivity(i);//, Global.ACTIVITY_FINISH);
									
									Button btn = (Button) Start.this.findViewById(R.id.record);
									btn.setText(Start.RECORDING_START);
									
									recording = !recording;											
								}
							})
						.setNegativeButton("No", null).show();
				
				
			}else {
				Button btn = (Button) v;
				btn.setText(Start.RECORDING_STOP);
				recording = !recording;
			}
			
			
		}
	}
    
    protected Dialog onCreateDialog(int id){
		switch(id) {
		case Start.DIALOG_OPTIONS:
			return new ListMenu(this, new OptionsListener(), options);
		}
		return null;
	}
    
    private class OptionsListener implements ListMenu.ListMenuListener {
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Log.d("TEST INDEX: ", "INDEX: " + arg2);
			if (arg2 == 0) {
				//HISTORY
			} else if (arg2 == 4){
				//EXIT
				locListener.stopListening();
				finish();
			}
		}
    }
    
    @Override
    protected boolean isRouteDisplayed() {
        return true;
    }
}